class Solution {
public:
    string reverseWords(string s) {
        string res;
        size_t size_pos =0,size_prev_pos =0;

        while((size_pos = s.find_first_of(" ",size_pos)) != string::npos )
        {
            string tmp = s.substr(size_prev_pos,size_pos-size_prev_pos);
            for(auto it = tmp.rbegin(); it != tmp.rend();++it)
            {
                res += *it;
            }
            res += ' ';
            size_prev_pos = ++size_pos;
        }
        if(size_prev_pos <= s.size())
        {
            string tmp = s.substr(size_prev_pos);
            for(auto it = tmp.rbegin(); it != tmp.rend();++it)
            {
                res += *it;
            }
        }
        return res;
    }
};
